<?php

	include("common.php");
	
	if(!empty($_POST)){
		mysql_query("UPDATE `197_users` SET `password` = '" . md5($_POST['password']) . "', `email` = '" . $_POST['email'] . "', `type` = " . $_POST['type'] . ", `realname` = '" . $_POST['realname'] . "' WHERE `user_id` = " . $_POST['user_id']);
		include("header.php");
		
?>
<?php if(!AJAX){ ?><h1>修改帳號</h1><?php } ?>
<p>修改成功<?php if(!AJAX){ ?>，請<a href="userFind.php">按此</a>返回<?php } ?>。</p>
<?php

		include("sidebar.php");
		include("footer.php");
		exit;
		
	}
	
	$result = mysql_query("SELECT * FROM `197_users` WHERE `user_id` = " . $_GET['user_id']);
	if(!mysql_num_rows($result)){
		include("header.php");
		
?>
<h1>修改帳號</h1>
<p>查無此帳號，請<a href="userFind.php">按此</a>返回。</p>
<?php

		include("sidebar.php");
		include("footer.php");
		
		exit;
		
	}

	$user = mysql_fetch_assoc($result);
	
?>
<?php include("header.php"); ?>
<h1>修改帳號</h1>
<div id="edit-result-dialog">
</div>
<script type="text/javascript" defer="defer">
	$("#edit-result-dialog").dialog({
		width: 300,
		height: 210,
		autoOpen: false,
		title: "修改帳號",
		modal: true,
		draggable: false,
		resizable: false
	});
	function editUser(){
		$("#edit-result-dialog").dialog("option", "buttons", { "關閉": function(){ location.replace("userFind.php"); } });
		$("#edit-result-dialog").html("<img src='../images/ajax-loader.gif' />");
		$("#edit-result-dialog").dialog("open");
		$.post("userEdit.php?ajax=true", { password: $("#password").val(), email: $("#email").val(), type: $("#type").val(), user_id: $("#user_id").val(), realname: $("#realname").val() }, function(r){ $("#edit-result-dialog").html(r) });
	}
</script>
<form action="userEdit.php" onsubmit="editUser(); return false;" method="POST">
	<p>帳號：<?php echo $user['username']; ?><input type="hidden" name="user_id" value="<?php echo $user['user_id']; ?>" id="user_id" /></p>
	<p><label>密碼：<input type="password" name="password" id="password" /></label></p>
	<p><label>信箱：<input type="text" name="email" value="<?php echo $user['email']; ?>" id="email" /></label></p>
	<p><label>姓名：<input type="text" name="realname" value="<?php echo $user['realname']; ?>" id="realname" /></label></p>
	<p><label>類型：<select name="type" id="type"><option value="1"<?php if($user['type'] == 1){ ?> selected="selected"<?php } ?>>一般</option><option value="2"<?php if($user['type'] == 2){ ?> selected="selected"<?php } ?>>管理員</option></select></label></p>
	<p><input type="submit" value="修改" />｜<a href="userFind.php">返回</a></p>
</form>
<?php include("sidebar.php"); ?>
<?php include("footer.php"); ?>